/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mnstudent.presentationlayer;

import mnstudent.global.MyTableCellRender;
import mnstudent.global.DataTable;
import mnstudent.global.Standard;
import mnstudent.businessLayer.SemesterBLL;
import mnstudent.datatranferobject.SemesterDTO;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author trungpv
 */
public class frmSemester extends javax.swing.JPanel {

    private SemesterBLL m_SemesterBLL;

    private DefaultTableModel m_Model;

    private int currentRow = 0;

    /**
     * Creates new form frmSemester
     */
    public frmSemester() {
        initComponents();
        initControls();
        prepare();
    }

    /**
     * Init Controls
     */
    private void initControls() {
        jxTxtSemesterID.setEditable(false);//luon luon false
        jxtabDetails.setEditable(false);
        
        //neu chua them moi thi EditTable = false
        if (!this.jxtabDetails.m_isAdd && !this.jxtabDetails.m_isUpdate) {
            
            jxTxtNameOfSemester.setEditable(false);
            jxbtnSave.setEnabled(false);
            jxBtnCancel.setEnabled(false);

            jxbtnAdd.setEnabled(true);
            jxbtnUpdate.setEnabled(true);
            jxbtnDelete.setEnabled(true);
        } else {
            
            jxTxtNameOfSemester.setEditable(true);
            jxbtnSave.setEnabled(true);
            jxBtnCancel.setEnabled(true);

            jxbtnAdd.setEnabled(false);
            jxbtnUpdate.setEnabled(false);
            jxbtnDelete.setEnabled(false);
        }
    }

    /**
     * Prepare
     */
    private void prepare() {

         //TextField tinh/quan 
        jxTxtNameOfSemester.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    int len = jxTxtNameOfSemester.getText().length();
                    if (len > Standard.MAX_LEN) {
                            e.consume();
                    }
                }
        });
        
        //Set lai text cac button cua dialog
        UIManager.put("OptionPane.cancelButtonText", "Hủy");
        UIManager.put("OptionPane.noButtonText", "Không");
        UIManager.put("OptionPane.okButtonText", "Có");
        UIManager.put("OptionPane.yesButtonText", "Vâng");
        //
        this.m_SemesterBLL = new SemesterBLL();

        m_Model = (DefaultTableModel) jxtabDetails.getModel();
        DataTable.addRow(m_SemesterBLL.getAllSemester(), m_Model);

        //get selected item
        jxtabDetails.setDefaultRenderer(Object.class, new MyTableCellRender());
        jxtabDetails.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent lse) {

                if (!(jxtabDetails.getSelectionModel().isSelectionEmpty())) {
                    //retrieving the selected row index
                    currentRow = jxtabDetails.getSelectedRow();
                    //if a single row is selected from the table, take each cell values into the controls
                    if (jxtabDetails.getRowSelectionAllowed() && jxtabDetails.getRowCount() > 0) {
                        if (jxtabDetails.m_isAdd || jxtabDetails.m_isUpdate) {
                            int rs = JOptionPane.showOptionDialog(null,
                                    "Vui lòng lưu thay đổi trước khi chuyển sang dòng khác!",
                                    "Cảnh báo",
                                    JOptionPane.YES_NO_OPTION,
                                    JOptionPane.QUESTION_MESSAGE,
                                    null,
                                    null,
                                    null);

                            if (rs == 0) {
                                //Yes
                                save();
                            } else {
                                cancelAction();
                            }
                        } else {
                            String strSemesterID = jxtabDetails.getValueAt(currentRow, 0).toString();
                            String strNameOfSemester = jxtabDetails.getValueAt(currentRow, 1).toString();
                            jxTxtSemesterID.setText(strSemesterID);
                            jxTxtNameOfSemester.setText(strNameOfSemester);
                        }
                    }
                }
            }
        });
        //neu co dl thi do mac dinh row dau tien
        if (jxtabDetails.getRowCount() > 0) {
            jxtabDetails.setRequestFocusEnabled(true);
            jxtabDetails.requestFocus();
            jxtabDetails.changeSelection(jxtabDetails.getRowCount() - 1, jxtabDetails.getRowCount() - 1, false, false);
        }
    }

    /**
     * Refresh Data Table
     */
    private void refreshDataInJTable() {
        m_Model.fireTableDataChanged();
        m_Model.fireTableStructureChanged();
        m_Model.getDataVector().removeAllElements();
        DataTable.addRow(m_SemesterBLL.getAllSemester(), m_Model);
        if (jxtabDetails.getRowCount() > 0) {
            jxtabDetails.setRequestFocusEnabled(true);
            jxtabDetails.requestFocus();
            jxtabDetails.changeSelection(jxtabDetails.getRowCount() - 1, jxtabDetails.getRowCount() - 1, false, false);
        }
    }

    /**
     * Ham xoa 1 HOCKY dc chon
     *
     * @param rowSelected
     * @return
     */
    private boolean isDelete(int rowSelected) {
        boolean m_isDelete = false;

        String id = this.jxtabDetails.getValueAt(rowSelected, 0).toString();
        String name = this.jxtabDetails.getValueAt(rowSelected, 1).toString();

        SemesterDTO semesterObj = new SemesterDTO(id, name);
        if (m_SemesterBLL.deleteSemesterFromDatabase(semesterObj)) {
            m_Model.removeRow(rowSelected);
            m_isDelete = true;
        }

        return m_isDelete;
    }

    /**
     * Ham xoa 1 luc nhieu HOCKY
     *
     * @return
     */
    private boolean isDeleteMoreSemester() {
        boolean m_isDelete = true;
        int[] arrRowSelected = this.jxtabDetails.getSelectedRows();
        for (int i = arrRowSelected.length - 1; i >= 0; i--) {
            if (!isDelete(arrRowSelected[i])) {
                JOptionPane.showMessageDialog(null,
                        "Xóa thất bại học kỳ: " + jxtabDetails.getValueAt(arrRowSelected[i], 1),
                        "Lỗi",
                        JOptionPane.YES_OPTION);
                m_isDelete = false;
            }
        }
        return m_isDelete;
    }

    /**
     * Ham thuc hien viec cap nhat va update
     */
    private void save() {
        if (this.jxtabDetails.m_isAdd) {
           saveToDatabase();
                
        } else if (this.jxtabDetails.m_isUpdate) {
            updateToDatabase();
        }
    }

    /**
     * Ham thuc hien insert HOCKY xuong database
     *
     * @return
     */
    private void saveToDatabase() {
        if (!this.jxTxtNameOfSemester.getText().equals("")) {
            String id = "HK0000***";
            String name = Standard.standardName(this.jxTxtNameOfSemester.getText());

            SemesterDTO semesterObj = new SemesterDTO(id, name);
            if (m_SemesterBLL.insertSemesterToDatabase(semesterObj)) {
                JOptionPane.showOptionDialog(null,
                        "Lưu xuống thành công!",
                        "Thông báo",
                        JOptionPane.YES_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        null,
                        null);
                //neu luu thanh cong
                this.jxtabDetails.m_isAdd = false;
                initControls();
                refreshDataInJTable();
            } else {
                JOptionPane.showMessageDialog(null, "Lưu xuống thất bại. Vui lòng kiểm tra lại!", "Lỗi", JOptionPane.YES_OPTION);
            }
        } 
    }

    /**
     * Ham thuc hien cap nhat HOCKY
     *
     * @return
     */
    private void updateToDatabase() {

        if (checkData()) {
            String id = this.jxTxtSemesterID.getText().trim();
            String name = Standard.standardName(this.jxTxtNameOfSemester.getText());

            SemesterDTO semesterObj = new SemesterDTO(id, name);
            if (m_SemesterBLL.updateSemesterToDatabase(semesterObj)) {
                JOptionPane.showOptionDialog(null,
                        "Câp nhật thành công!",
                        "Thông báo",
                        JOptionPane.YES_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        null,
                        null);
                //neu Cập nhật thanh cong
                this.jxtabDetails.m_isUpdate = false;
                initControls();
                refreshDataInJTable();
            } else {
                JOptionPane.showMessageDialog(null, "Cập nhật thất bại. Vui lòng kiểm tra lại!", "Lỗi", JOptionPane.YES_OPTION);
            }
        }
    }

    private void cancelAction() {
        if (this.jxtabDetails.m_isAdd) {
            ((DefaultTableModel) jxtabDetails.getModel()).removeRow(jxtabDetails.getRowCount() - 1);
            this.jxtabDetails.m_isAdd = false;
            initControls();
            jxTxtSemesterID.setText("");
            jxTxtNameOfSemester.setText("");
            jxtabDetails.changeSelection(jxtabDetails.getRowCount() - 1, jxtabDetails.getRowCount() - 1, false, false);

        } else {
            this.jxtabDetails.m_isUpdate = false;
             initControls();
        }
        refreshDataInJTable();
    }
    private boolean checkData()
     {
           if(this.jxTxtNameOfSemester.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Tên học kỳ không được bỏ trống!");
            return false;
        }
        else
        {
            if(!m_SemesterBLL.checkInputIsInvalid(this.jxtabDetails, this.jxTxtNameOfSemester.getText()))
            {
                JOptionPane.showMessageDialog(null, "Học kỳ vừa nhập đã có trong cơ sở dữ liệu!");
                return false;
            }
        }
        return true;
         
     }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jxpanDetails = new org.jdesktop.swingx.JXPanel();
        jpanTableDetails = new javax.swing.JPanel();
        jpanContent = new javax.swing.JPanel();
        jxpanFindNavibar = new org.jdesktop.swingx.JXPanel();
        jpanChose = new javax.swing.JPanel();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jxbtnFindVisible = new org.jdesktop.swingx.JXButton();
        jxSearchSemester = new org.jdesktop.swingx.JXSearchField();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jxtabDetails = new mnstudent.global.MyTable();
        jpanFuntionalDetails = new javax.swing.JPanel();
        jxbtnAdd = new org.jdesktop.swingx.JXButton();
        jxbtnUpdate = new org.jdesktop.swingx.JXButton();
        jxbtnDelete = new org.jdesktop.swingx.JXButton();
        jxbtnSave = new org.jdesktop.swingx.JXButton();
        jxBtnCancel = new org.jdesktop.swingx.JXButton();
        jxpanHeader = new org.jdesktop.swingx.JXPanel();
        jxpanTitleHeader = new org.jdesktop.swingx.JXPanel();
        jXLabel5 = new org.jdesktop.swingx.JXLabel();
        jxpanInfoHeader = new org.jdesktop.swingx.JXPanel();
        jxpanInfo = new org.jdesktop.swingx.JXPanel();
        jxLabFacultyID = new org.jdesktop.swingx.JXLabel();
        jxTxtSemesterID = new org.jdesktop.swingx.JXTextField();
        jxLabNameOfFaculty = new org.jdesktop.swingx.JXLabel();
        jxTxtNameOfSemester = new org.jdesktop.swingx.JXTextField();

        setLayout(new java.awt.BorderLayout());

        jxpanDetails.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jxpanDetails.setLayout(new java.awt.BorderLayout());

        jpanTableDetails.setLayout(new java.awt.BorderLayout());

        jpanContent.setLayout(new java.awt.BorderLayout());

        jxpanFindNavibar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jxpanFindNavibar.setPreferredSize(new java.awt.Dimension(914, 30));

        jpanChose.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn nội dung tìm kiếm"));

        jCheckBox3.setText("Tìm theo mã học kỳ");

        jCheckBox1.setText("Tìm kiếm nhanh");

        jCheckBox4.setText("Tìm theo tên học kỳ");

        javax.swing.GroupLayout jpanChoseLayout = new javax.swing.GroupLayout(jpanChose);
        jpanChose.setLayout(jpanChoseLayout);
        jpanChoseLayout.setHorizontalGroup(
            jpanChoseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanChoseLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jCheckBox3)
                .addGap(65, 65, 65)
                .addComponent(jCheckBox4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 394, Short.MAX_VALUE)
                .addComponent(jCheckBox1)
                .addGap(105, 105, 105))
        );
        jpanChoseLayout.setVerticalGroup(
            jpanChoseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanChoseLayout.createSequentialGroup()
                .addGroup(jpanChoseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox3)
                    .addComponent(jCheckBox1)
                    .addComponent(jCheckBox4))
                .addGap(0, 0, 0))
        );

        jxbtnFindVisible.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jxbtnFindVisible.setText("Tùy chọn");
        jxbtnFindVisible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxbtnFindVisibleActionPerformed(evt);
            }
        });

        jXLabel1.setText("Tìm kiếm:");

        javax.swing.GroupLayout jxpanFindNavibarLayout = new javax.swing.GroupLayout(jxpanFindNavibar);
        jxpanFindNavibar.setLayout(jxpanFindNavibarLayout);
        jxpanFindNavibarLayout.setHorizontalGroup(
            jxpanFindNavibarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jxpanFindNavibarLayout.createSequentialGroup()
                .addComponent(jxbtnFindVisible, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jxSearchSemester, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jpanChose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jxpanFindNavibarLayout.setVerticalGroup(
            jxpanFindNavibarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxpanFindNavibarLayout.createSequentialGroup()
                .addGroup(jxpanFindNavibarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jxbtnFindVisible, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxSearchSemester, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpanChose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpanContent.add(jxpanFindNavibar, java.awt.BorderLayout.NORTH);

        jxtabDetails.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã học kỳ", "Tên học kỳ"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jxtabDetails);

        jpanContent.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jpanTableDetails.add(jpanContent, java.awt.BorderLayout.CENTER);

        jpanFuntionalDetails.setLayout(new java.awt.GridBagLayout());

        jxbtnAdd.setText("Thêm mới");
        jxbtnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxbtnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 6, 9);
        jpanFuntionalDetails.add(jxbtnAdd, gridBagConstraints);

        jxbtnUpdate.setText("Sửa đổi");
        jxbtnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxbtnUpdateActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 6, 8);
        jpanFuntionalDetails.add(jxbtnUpdate, gridBagConstraints);

        jxbtnDelete.setText("Xóa bỏ");
        jxbtnDelete.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                jxbtnDeleteAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jxbtnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxbtnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 4, 8);
        jpanFuntionalDetails.add(jxbtnDelete, gridBagConstraints);

        jxbtnSave.setText("Lưu xuống");
        jxbtnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxbtnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(11, 9, 7, 8);
        jpanFuntionalDetails.add(jxbtnSave, gridBagConstraints);

        jxBtnCancel.setText("Hủy bỏ");
        jxBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(12, 9, 8, 9);
        jpanFuntionalDetails.add(jxBtnCancel, gridBagConstraints);

        jpanTableDetails.add(jpanFuntionalDetails, java.awt.BorderLayout.EAST);

        jxpanDetails.add(jpanTableDetails, java.awt.BorderLayout.CENTER);

        add(jxpanDetails, java.awt.BorderLayout.CENTER);

        jxpanHeader.setLayout(new java.awt.BorderLayout());

        jxpanTitleHeader.setBorder(new org.jdesktop.swingx.border.DropShadowBorder());

        jXLabel5.setForeground(new java.awt.Color(0, 102, 255));
        jXLabel5.setText("HỌC KỲ");
        jXLabel5.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jxpanTitleHeader.add(jXLabel5);

        jxpanHeader.add(jxpanTitleHeader, java.awt.BorderLayout.PAGE_START);

        jxpanInfoHeader.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jxpanInfoHeader.setLayout(new java.awt.BorderLayout());

        jxpanInfo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "THÔNG TIN", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP));

        jxLabFacultyID.setText("Mã học kỳ");

        jxTxtSemesterID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jxTxtSemesterIDCaretUpdate(evt);
            }
        });

        jxLabNameOfFaculty.setText("Tên học kỳ");

        jxTxtNameOfSemester.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jxTxtNameOfSemesterCaretUpdate(evt);
            }
        });

        javax.swing.GroupLayout jxpanInfoLayout = new javax.swing.GroupLayout(jxpanInfo);
        jxpanInfo.setLayout(jxpanInfoLayout);
        jxpanInfoLayout.setHorizontalGroup(
            jxpanInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxpanInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jxLabFacultyID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jxTxtSemesterID, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62)
                .addComponent(jxLabNameOfFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jxTxtNameOfSemester, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(437, Short.MAX_VALUE))
        );
        jxpanInfoLayout.setVerticalGroup(
            jxpanInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxpanInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jxpanInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jxLabFacultyID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxTxtSemesterID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxLabNameOfFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxTxtNameOfSemester, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jxpanInfoHeader.add(jxpanInfo, java.awt.BorderLayout.CENTER);

        jxpanHeader.add(jxpanInfoHeader, java.awt.BorderLayout.CENTER);

        add(jxpanHeader, java.awt.BorderLayout.NORTH);
    }// </editor-fold>//GEN-END:initComponents

    private void jxbtnFindVisibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxbtnFindVisibleActionPerformed
        if (jxbtnFindVisible.getText().equals("Đóng")) {
            jxbtnFindVisible.setPreferredSize(new Dimension(110, 25));
            jxbtnFindVisible.setText("Tùy chọn");
            jxbtnFindVisible.revalidate();
            jxpanFindNavibar.setPreferredSize(new Dimension(30, 30));
            jxpanFindNavibar.revalidate();
        } else {
            jxbtnFindVisible.setPreferredSize(new Dimension(20, 25));
            jxbtnFindVisible.setText("Đóng");
            jxbtnFindVisible.revalidate();
            jxpanFindNavibar.setPreferredSize(new Dimension(30, 105));
            jxpanFindNavibar.revalidate();
        }
    }//GEN-LAST:event_jxbtnFindVisibleActionPerformed

    private void jxbtnDeleteAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_jxbtnDeleteAncestorAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_jxbtnDeleteAncestorAdded

    private void jxbtnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxbtnAddActionPerformed
        for (int i = 0; i < jxtabDetails.getColumnCount(); i++) {
            if (jxtabDetails.getValueAt(jxtabDetails.getRowCount() - 1, i).equals("")) {
                return;
            }
        }

        Vector newRow = new Vector(jxtabDetails.getColumnCount());
        for (int i = 0; i < jxtabDetails.getColumnCount(); i++) {
            newRow.addElement("");
        }
        ((DefaultTableModel) jxtabDetails.getModel()).addRow(newRow);
        jxtabDetails.setRowSelectionInterval(jxtabDetails.getRowCount() - 1, jxtabDetails.getRowCount() - 1);
        //jxtabDetails.setEnabled(false);
        //jxtabDetails.setRowSelectionAllowed(false);
        JScrollBar sb = jScrollPane2.getVerticalScrollBar();
        sb.setValue(sb.getMaximum());

        if (!this.jxtabDetails.m_isAdd) {
            this.jxtabDetails.m_isAdd = true;
            initControls();
            this.jxtabDetails.m_rowIndex = jxtabDetails.getRowCount() - 1;
            jxTxtSemesterID.setText("HK0000***");
            jxTxtNameOfSemester.requestFocus();
        }
    }//GEN-LAST:event_jxbtnAddActionPerformed

    private void jxTxtSemesterIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jxTxtSemesterIDCaretUpdate
        //jxtabDetails.getModel().setValueAt(jxTxtSemesterID.getText(), jxtabDetails.getRowCount() - 1, 0);

        if (this.jxtabDetails.m_isAdd) {
            jxtabDetails.getModel().setValueAt(jxTxtSemesterID.getText(), jxtabDetails.getRowCount() - 1, 0);
        } else if (this.jxtabDetails.m_isUpdate) {
            jxtabDetails.getModel().setValueAt(jxTxtSemesterID.getText(), jxtabDetails.getSelectedRow(), 0);
        }

    }//GEN-LAST:event_jxTxtSemesterIDCaretUpdate

    private void jxTxtNameOfSemesterCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jxTxtNameOfSemesterCaretUpdate
        //jxtabDetails.getModel().setValueAt(jxTxtNameOfSemester.getText(), jxtabDetails.getRowCount() - 1, 1);
        if (this.jxtabDetails.m_isAdd) {
            jxtabDetails.getModel().setValueAt(jxTxtNameOfSemester.getText(), jxtabDetails.getRowCount() - 1, 1);
        } else if (this.jxtabDetails.m_isUpdate) {
            jxtabDetails.getModel().setValueAt(jxTxtNameOfSemester.getText(), jxtabDetails.getSelectedRow(), 1);
        }
    }//GEN-LAST:event_jxTxtNameOfSemesterCaretUpdate

    private void jxbtnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxbtnUpdateActionPerformed
        jxbtnAdd.setEnabled(false);
        jxbtnDelete.setEnabled(false);
        jxbtnUpdate.setEnabled(false);

        jxbtnSave.setEnabled(true);
        jxBtnCancel.setEnabled(true);
        jxTxtNameOfSemester.setEditable(true);
        jxTxtNameOfSemester.requestFocus();

        this.jxtabDetails.m_isUpdate = true;
    }//GEN-LAST:event_jxbtnUpdateActionPerformed

    private void jxbtnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxbtnDeleteActionPerformed
        int rs = JOptionPane.showOptionDialog(null,
                "Bạn có muốn xóa không!",
                "Câu hỏi",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,
                null,
                null);
        if (rs == 0) {
            if (isDeleteMoreSemester()) {
                JOptionPane.showOptionDialog(null,
                        "Xóa thành công!",
                        "Thông báo",
                        JOptionPane.YES_OPTION,
                        JOptionPane.INFORMATION_MESSAGE,
                        null,
                        null,
                        null);
                //neu luu thanh cong
                this.jxtabDetails.m_isAdd = false;
                initControls();
                refreshDataInJTable();
            } else {
                JOptionPane.showMessageDialog(null, "Xóa thất bại. Vui lòng kiểm tra lại!", "Lỗi", JOptionPane.YES_OPTION);
            }
        }
    }//GEN-LAST:event_jxbtnDeleteActionPerformed

    private void jxbtnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxbtnSaveActionPerformed
        save();
    }//GEN-LAST:event_jxbtnSaveActionPerformed

    private void jxBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnCancelActionPerformed
        cancelAction();
    }//GEN-LAST:event_jxBtnCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel5;
    private javax.swing.JPanel jpanChose;
    private javax.swing.JPanel jpanContent;
    private javax.swing.JPanel jpanFuntionalDetails;
    private javax.swing.JPanel jpanTableDetails;
    private org.jdesktop.swingx.JXButton jxBtnCancel;
    private org.jdesktop.swingx.JXLabel jxLabFacultyID;
    private org.jdesktop.swingx.JXLabel jxLabNameOfFaculty;
    private org.jdesktop.swingx.JXSearchField jxSearchSemester;
    private org.jdesktop.swingx.JXTextField jxTxtNameOfSemester;
    private org.jdesktop.swingx.JXTextField jxTxtSemesterID;
    private org.jdesktop.swingx.JXButton jxbtnAdd;
    private org.jdesktop.swingx.JXButton jxbtnDelete;
    private org.jdesktop.swingx.JXButton jxbtnFindVisible;
    private org.jdesktop.swingx.JXButton jxbtnSave;
    private org.jdesktop.swingx.JXButton jxbtnUpdate;
    private org.jdesktop.swingx.JXPanel jxpanDetails;
    private org.jdesktop.swingx.JXPanel jxpanFindNavibar;
    private org.jdesktop.swingx.JXPanel jxpanHeader;
    private org.jdesktop.swingx.JXPanel jxpanInfo;
    private org.jdesktop.swingx.JXPanel jxpanInfoHeader;
    private org.jdesktop.swingx.JXPanel jxpanTitleHeader;
    private mnstudent.global.MyTable jxtabDetails;
    // End of variables declaration//GEN-END:variables
}
